package com.snopy.sorts.selection;

/**
 * @author snopy
 * @description 选择排序
 * @classname Selection
 * @date 2021/02/02 12:54
 * @email 77912204@qq.com
 */
public class Selection {

    public  static void sort(Comparable[] arr){
        for (int i = 0;i<=arr.length-2 ; i++){
            int minIndex = i;
            for (int j = i+1;j<arr.length;j++){
                if (greater(arr[minIndex],arr[j])){
                    minIndex = j;
                }
            }
            exch(arr,i,minIndex);
        }
    }
    /**
      *<p> 比较v元素是否大于w元素</p>
      *@param
      *@return
      *@author snopy
      *@date 2021/2/2 12:58
      */

    private static boolean greater(Comparable v,Comparable w){
        return  v.compareTo(w) > 0;
    }
    /**
      *<p> 交换元素i和j的位置</p>
      *@param
      *@return
      *@author snopy
      *@date 2021/2/2 13:00
      */
    private static void exch(Comparable[] a,int i,int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

}
